#唐门优测
#道长最帅666
""
"""
列表List
列表list用[,...]表示，格式如下
nameList = [tangsan,xiaowu,xiaoming,daming]

"""
"for循环遍历list"
"例子"
nameList = ['tangsan','xiaowu','xiaoming','daming']
for name in nameList:
    print(name)

"列表推导式"
"普通方式"
a = [x for x in range(4)]
print(a)
a = [x for x in range(3,4)]
print(a)
a = [x for x in range(3,19)]
print(a)
a = [x for x in range(3,19,4)]
print(a)

"在循环的过程中加入if判断"
a = [x for x in range(3,15) if x%2 ==0]
print(a)
a = [x for x in range(3,15) if x%2 !=0]
print(a)
a = [x for x in range(3,15)]
print(a)

"多个for循环"
a = [(x,y) for x in range(3,5) for y in range(3)]
print(a)
a = [(x,y,z) for x in range(3,5) for y in range(3) for z in range(4,6)]
print(a)

"while循环遍历list"
nameList = ['tangsan','xiaowu','xiaoming','daming']
length = len(nameList)
print(length)
i = 0
while i < length:
    print(nameList[i])
    i+=1

"列表常用操作 增删改"
"添加元素：通过append可以向列表添加元素"
"例子"
# A = ['tangsan','xiaowu','xiaoming','daming']
# print("--添加之前，列表A的数据---")
# for B in A:
#     print(B)
# #提示，并添加
# temp = input("请输入姓名：")
# A.append(temp)
# print("--添加之后，列表A的数据---")
# for B in A:
#     print(B)

"extend: 通过extend可以将另一个集合中的元素逐一添加到列表中"
"例子"
a = [1,2]
b = [3,4]
a.append(b)
print(a)
a.extend(b)
print(a)

"insert :insert(index,object)在指定位置index前插入元素object"
a = [0,1,2]
a.insert(1,3)
print(a)

"修改元素（改）"
"修改元素的时候，要通过下标来确定要修改的是哪个元素，然后才能进行修改，例子"
# A = ['tangsan','xiaowu','xiaoming','daming']
# print("--添加之前，列表A的数据---")
# for B in A:
#     print(B)
# #提示，并添加
# A[2] = "tianzun"
# print("--添加之后，列表A的数据---")
# for B in A:
#     print(B)

"查找元素"
"in ,not in , index count"
"in (存在)，如果存在那么结果为TRUE，否则false"
"not in (不存在)，如果不存在那么结果为TRUE，否则false"

# A = ['tangsan','xiaowu','xiaoming','daming']
# x = input("请输入姓名：")
# if x in A:
#     print("在字典中找到了相同的名字")
# else:
#     print("没有找到")

"index count"
a = ['a','b','c','a','b']
print(a.index('a',1,4))
print(a.count('b'))
print(a.count('d'))
# print(a.index('a',1,3))

"删除元素 删 del pop remove"
"""
del ：根据下标进行删除
pop ：删除最后一个元素
remove :根据元素的值进行删除
"""
"del"
# A = ['tangsan','xiaowu','xiaoming','daming']
# print("删除之前")
# for x in A:
#     print(x)
# del A[0]
# print("删除之后")
# for x in A:
#     print(x)

"pop"
# A = ['tangsan','xiaowu','xiaoming','daming']
# print("删除之前")
# for x in A:
#     print(x)
# A.pop()
# print("删除之后")
# for x in A:
#     print(x)

"remove"
A = ['tangsan','xiaowu','xiaoming','daming']
print("删除之前")
for x in A:
    print(x)
A.remove("xiaoming")
print("删除之后")
for x in A:
    print(x)

"排序(sort,reverse)"
"sort方法是将list按特点顺序重新排列，默认为有小到大，参数reverse=True可改为倒序，由大到小"
a = [1,4,2,3]
a.reverse()
print(a)
a.sort()
print(a)
a.sort(reverse=False)
print(a)

"列表的嵌套"
a = [[1,4,2,3],[1,4,5,9]]

"元组 python的元组和列表类似，不同之处在于 元组的元素不能修改。元组使用小括号()，列表使用方括号，例子"
aTuple = ('daozhang',77,99.9)
print(aTuple)

"访问元组"
aTuple = ('daozhang',77,99.9)
print(aTuple[0])
print(aTuple[2])

# "修改元组"
# aTuple = ('daozhang',77,99.9)
# aTuple[0] = "tangsan"

"index count"

"字典  d = {key1 :value2,key2:value2}"

"根据键访问值"
info = {"name":"tangsan1","id":100,"name":"f","address":"杭州"}
print(info['name'])
print(info["address"])
# print(info["address1"])

"我们不确定字典字典中是否存在某个键而又想获取其值时，可以使用get方法，还可以设置默认值"
# age = info.get("id1")
# print(age)
# print(type(age))

age = info.get("id1",18)#若这个键不存在，返回默认18
print(age)


"修改元素"
info = {"name":"tangsan1","id":100,"name":"f","address":"杭州"}
info[id] = 798.
print(info[id])

"添加元素"
# info = {"name":"tangsan1","id":100,"name":"f","address":"杭州"}
# newid = input("请输入新的ID：")
# info['id'] = int(newid)
# print(info['id'])

"del clear()删除"
"del 删除指定元素"
# info = {"name":"tangsan1","id":100,"name":"f","address":"杭州"}
# print(info)
# del info
# print("删除后",info)

"clear()清空整个字典"
info = {"name":"tangsan1","id":100,"name":"f","address":"杭州"}
print(info)
info.clear()
print("删除后",info)

"字典的长度"
"len"
info = {"name":"tangsan1","id":100,"name":"f","address":"杭州"}
print("字典的长度：",len(info))


"找出字典中的所有key"
"keys返回一个包含字典所有key的视图对象"
info = {"name":"tangsan1","id":100,"name":"f","address":"杭州"}
dickeys = list(info.keys())
print(dickeys)
print(dickeys[0])

"找出字典中的所有value"
"属性value返回一个包含字典所有value的试图列表，"
info = {"name":"tangsan1","id":100,"name":"f","address":"杭州"}
dickeys = list(info.values())
print(dickeys)
print(dickeys[0])

"找出字典中的(键和值)"
info = {"name":"tangsan1","id":100,"name":"f","address":"杭州"}
dicItems = info.items()
print(dicItems)

"判断key是否存在"
info = {"name":"tangsan1","id":100,"name":"f","address":"杭州"}
ss = "name" in info
print(ss)

"遍历字典的几种方式"
"遍历字典的key"
info = {"name":"tangsan1","id":100,"name":"f","address":"杭州"}
for key in info.keys():
    print(key)

"遍历字典的value"
info = {"name":"tangsan1","id":100,"name":"f","address":"杭州"}
for key in info.values():
    print(key)

"遍历字典的项"
info = {"name":"tangsan1","id":100,"name":"f","address":"杭州"}
for item in info.items():
    print(item)

"遍历字典的键值对"
info = {"name":"tangsan1","id":100,"name":"f","address":"杭州"}
for key,value in info.items():
    print(key,value)

"作业"
"字符串常见操作"
"""
find、index、count、replace、split、title、startwith、endwith、lower、upper、lstrip、rstrip、rfind、rindex、join、ljust

"""